package com.example.dessert.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dessert.dao.AddressMapper;
import com.example.dessert.entity.Address;
import com.example.dessert.service.AddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;

    @Override
    public List<Address> queryAddressList(Long user_id) {

        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        if(user_id != null){
            wrapper.eq("user_id",user_id);
        }
        wrapper.orderByDesc("created_at");
        List<Address> address = addressMapper.selectList(wrapper);

        return address;
    }


    @Override
    public Address queryAddressById(Long id) {

        return addressMapper.selectById(id);

    }


    @Override
    public Long storeAddress(Address address) {

        addressMapper.insert(address);
        return  address.getId();

    }

    @Override
    public int saveAddress(Address address) {

        int result = addressMapper.updateById(address);
        return  result;

    }

    @Override
    public int deleteAddressById(Long id) {

        int result = addressMapper.deleteById(id);
        return  result;

    }


}
